<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">

    <link rel="stylesheet" href="../../../static/layui2/css/layui.css" media="all"/>
    <link rel="stylesheet" href="../../../static/css/font_eolqem241z66flxr.css" media="all"/>
</head>
<body class="childrenBody">
<form class="layui-form" lay-filter="demo">
    <div class="layui-form-item">
        <label class="layui-form-label">标题</label>
        <div class="layui-input-block">
            <input type="text" name="name" class="layui-input newsName" lay-verify="required" placeholder="请输入标题">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">内容摘要</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入内容摘要" name="desc" id="desc" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">排序</label>
        <div class="layui-input-block">
            <input type="number" name="sort" maxlength="5" class="layui-input newsName" lay-verify="required"
                   placeholder="请输入排序">
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" type="button" lay-filter="formData">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script type="text/javascript" src="../../../static/layui2/layui.js"></script>
<script type="text/javascript" src="../../../static/js/queryUtils.js"></script>
<!--	<script type="text/javascript" src="demoAdd.js"></script>-->

<script>
    var layedit="";
    layui.use(['jquery', 'form', 'laydate', 'upload', 'layedit'], function () {
        var form = layui.form
            , layer = layui.layer
            , laydate = layui.laydate
            , $ = layui.jquery
            , upload = layui.upload;
      layedit = layui.layedit;
        var operateType = QueryUtils.GetQueryString("type");
        var id = QueryUtils.GetQueryString("id");
        var pid = QueryUtils.GetQueryString("pid");
        var active = {
            cancel: function () {
                // 当你在iframe页面关闭自身时
                var index = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引
                parent.layer.close(index); // 再执行关闭
            }
        };
        layedit.set({
            uploadImage: {
                 url: '/UploadController/uploadFile/'
            }
        });


       var index= layedit.build('desc', {
            tool: [
                'strong' //加粗
                , 'italic' //斜体
                , 'underline' //下划线
                , 'del' //删除线

                , '|' //分割线

                , 'left' //左对齐
                , 'center' //居中对齐
                , 'right' //右对齐
                , 'link' //超链接
                , 'unlink' //清除链接
                , 'face' //表情
                , 'image' //插入图片
                , 'help' //帮助
            ]

        });


        $('.operate-button-group .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        switch (operateType) {
            case "edit":
                $.ajax({
                    url: "/ArticleController/view",
                    data: {id: id},
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {

                        if (res.code === "00") {
                            //表单初始赋值
                            form.val('demo', {
                                "name": res.data.name
                                , "sort": res.data.sort
                            })
                            layedit.setContent(index,res.data.desc);
                            form.render();
                        } else if (res.code === "01") {
                            layer.msg(res.msg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
                break;

        }


        //监听提交
        form.on('submit(formData)', function (data) {
            debugger
            data.field.pid = pid;
            data.field.desc=  layedit.getContent(index)
            /*layer.alert(JSON.stringify(data.field), {
                title: '最终的提交信息'
            });*/
            if (operateType === "add") {
                layer.msg('确认保存信息吗', {
                    time: 0
                    , btn: ['确定', '取消']
                    , shade: 0.3
                    , yes: function (index) {
                        layer.close(index); // 关闭当前提示框
                        $.ajax({
                            url: "/ArticleController/add",
                            data: data.field,
                            dataType: "json",
                            type: "post",
                            async: false,
                            success: function (res) {

                                if (res.code === "00") {
                                    var i = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(i);  // 关闭新建页面
                                    parent.layui.table.reload('dataTable', {});    // 父页面表格刷新
                                    parent.active.renderTable();
                                    parent.layer.msg("保存成功", {offset: '150px'});   // 父页面弹出保存成功
                                } else if (res.code === "01") {
                                    layer.msg(res.msg, function () {
                                    });
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                console.error(XMLHttpRequest.status);
                                console.error(XMLHttpRequest.readyState);
                                console.error(textStatus);
                            }
                        });
                    }
                });
            }
            else if (operateType === "edit") {
                data.field.id = id;
                data.field.pid = pid;
                layer.msg('确认保存信息吗', {
                    time: 0
                    , btn: ['确定', '取消']
                    , shade: 0.3
                    , yes: function (index) {
                        layer.close(index); // 关闭当前提示框
                        $.ajax({
                            url: "/ArticleController/update",
                            data: data.field,
                            dataType: "json",
                            type: "post",
                            async: false,
                            success: function (res) {

                                if (res.code === "00") {
                                    var i = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(i);  // 关闭新建页面
                                    parent.layui.table.reload('dataTable', {});    // 父页面表格刷新
                                    parent.active.renderTable();
                                    parent.layer.msg("保存成功", {offset: '150px'});   // 父页面弹出保存成功
                                } else if (res.code === "01") {
                                    layer.msg(res.msg, function () {
                                    });
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                console.error(XMLHttpRequest.status);
                                console.error(XMLHttpRequest.readyState);
                                console.error(textStatus);
                            }
                        });
                    }
                });
            }
            return false;
        });

    });
</script>
</body>
</html>